home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
SPACE 2
/
SPACE - Library 2 - Volume 1.iso
/
games
/
111
/
fortune.c
< prev
next >
Wrap
C/C++ Source or Header
|
1987-02-19
|
2KB
|
83 lines
/* fortune.c Larn is copyrighted 1986 by Noah Morgan. */
#ifdef TOS
#include <fcntl.h>
#include <astat.h>
#else
#include <sys/types.h>
#include <sys/stat.h>
#ifndef BSD4.1
#include <fcntl.h>
#else BSD4.1
#define O_RDONLY 0
#endif BSD4.1
#endif
#include "header.h"
/*
* function to return a random fortune from the fortune file
*/
static char *base=0; /* pointer to the fortune text */
static char **flines=0; /* array of pointers to each fortune */
static int fd=0; /* true if we have load the fortune info */
static int nlines=0; /* # lines in fortune database */
char *fortune(file)
char *file;
{
register char *p;
register int lines,tmp;
struct stat stat;
char *malloc();
if (fd==0)
{
#ifdef TOS
if ((fd=open(file,O_RAW|O_RDONLY)) < 0) /* open the file */
#else
if ((fd=open(file,O_RDONLY)) < 0) /* open the file */
#endif
return(0); /* can't find file */
/* find out how big fortune file is and get memory for it */
stat.st_size = 16384;
#ifdef TOS
if ((getstat(file,&stat) < 0) ||
((base=malloc(1+stat.st_size)) == 0))
#else
if ((fstat(fd,&stat) < 0) || ((base=malloc(1+stat.st_size)) == 0))
#endif
{
close(fd); fd= -1; free((char*)base); return(0); /* can't stat file */
}
/* read in the entire fortune file */
if (read(fd,base,stat.st_size) != stat.st_size)
{
close(fd); fd= -1; free((char*)base); return(0); /* can't read file */
}
close(fd); base[stat.st_size]=0; /* final NULL termination */
/* count up all the lines (and NULL terminate) to know memory needs */
for (p=base,lines=0; p<base+stat.st_size; p++) /* count lines */
if (*p == '\n') *p=0,lines++;
nlines = lines;
/* get memory for array of pointers to each fortune */
if ((flines=(char**)malloc(nlines*sizeof(char*))) == 0)
{
free((char*)base); fd= -1; return(0); /* malloc() failure */
}
/* now assign each pointer to a line */
for (p=base,tmp=0; tmp<nlines; tmp++)
{
flines[tmp]=p; while (*p++); /* advance to next line */
}
}
if (fd > 2) /* if we have a database to look at */
return(flines[rund((nlines<=0)?1:nlines)]);
else
return(0);
}